package com.gameley.dmgtpf.common.utils;

import com.gameley.dmgtpf.common.base.SuperEntity;
import com.google.gson.Gson;
import org.springframework.ui.Model;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;

/**
 * *
 *
 * @version 1.0<br>
 * @qualiFild PubEnvUtil.java<br>
 * @author：LiBencheng<br>
 * @date Created on 2021年7月28日<br>
 */
public class PubEnvUtil extends ArrayUtil implements PubConsUtil
{

    public static String CODE = "code";
    public static String MSG = "msg";
    public static String DATA = "data";
    
    /**
     * @date 2021年7月26日
     */
    private static final long serialVersionUID = 3633962028688174872L;
    
    /**
     * *********************************************************** <br>
     * *说明： 获取表名<BR>
     *
     * @param superEntity
     * @return <BR>
     * @String <BR>
     * @methods PubEnvUtil#getTabmeName <BR>
     * @author LiBencheng <BR>
     * @date Created on 2021年7月28日 <BR>
     * @time 上午9:43:35 <BR>
     * @version 1.0 <BR>
     *          ************************************************************ <br>
     * @see <BR>
     */
    public static String getTabmeName(SuperEntity superEntity)
    {
        com.baomidou.mybatisplus.annotation.TableName table = (com.baomidou.mybatisplus.annotation.TableName) superEntity.getClass()
            .getAnnotation(com.baomidou.mybatisplus.annotation.TableName.class);
        
        return table.value();
    }
    
    /**
     * *********************************************************** <br>
     * *说明： 字符串加密函数MD5实现<BR>
     *
     * @param password
     * @return
     * @throws NoSuchAlgorithmException <BR>
     * @String <BR>
     * @methods PubEnvUtil.md5 <BR>
     * @author LiBencheng <BR>
     *         HXC067 <BR>
     * @date Created on 2021年7月28日 <BR>
     * @time 上午9:50:27 <BR>
     * @version 1.0 <BR>
     * @see <BR>
     *      ************************************************************ <br>
     */
    public final static String md5(String password) throws NoSuchAlgorithmException
    {
        MessageDigest md;
        // 生成一个MD5加密计算摘要
        md = MessageDigest.getInstance("MD5");
        // 计算md5函数
        md.update(password.getBytes());
        // digest()最后确定返回md5 hash值，返回值为8为字符串。因为md5 hash值是16位的hex值，实际上就是8位的字符
        // BigInteger函数则将8位的字符串转换成16位hex值，用字符串来表示；得到字符串形式的hash值
        String pwd = new BigInteger(1, md.digest()).toString(16);
        return pwd;
        
    }
    
    /**
     * *********************************************************** <br>
     * *说明： <BR>
     *
     * @param obj1
     * @param obj2
     * @return <BR>
     * @boolean <BR>
     * @methods PubEnvUtil.equals <BR>
     * @author LiBencheng <BR>
     *         HXC067 <BR>
     * @date Created on 2021年7月28日 <BR>
     * @time 上午9:52:06 <BR>
     * @version 1.0 <BR>
     * @see <BR>
     *      <p>
     *      ******************************************** **************** <br>
     */
    public static boolean equals(Object obj1, Object obj2)
    {
        try
        {
            boolean eq;
            
            if (obj1 == obj2) //
            {
                eq = true;
            }
            else if (obj1 == null && obj2 != null) //
            {
                eq = false;
            }
            else if (obj2 == null && obj1 != null) //
            {
                eq = false;
            }
            else if ((obj1 instanceof Integer || obj2 instanceof Integer)
                && Integer.parseInt(obj1.toString()) == Integer.parseInt(obj2.toString())) //
            {
                eq = true;
            }
            else if ((obj1 instanceof Double || obj2 instanceof Double)
                && Double.parseDouble(obj1.toString()) == Double.parseDouble(obj2.toString())) //
            {
                eq = true;
            }
            else if ((obj1 instanceof Long || obj2 instanceof Long) && Long.parseLong(obj1.toString()) == Long.parseLong(obj2.toString())) //
            {
                eq = true;
            }
            else if ((obj1 instanceof Short || obj2 instanceof Short)
                && Short.parseShort(obj1.toString()) == Short.parseShort(obj2.toString())) //
            {
                eq = true;
            }
            else if ((obj1 instanceof Float || obj2 instanceof Float)
                && Float.parseFloat(obj1.toString()) == Float.parseFloat(obj2.toString())) //
            {
                eq = true;
            }
            else if ((obj1 instanceof String || obj2 instanceof String) && obj1.toString().equals(obj2.toString())) //
            {
                eq = true;
            }
            else if (obj1 instanceof Object[] && obj2 instanceof Object[]) //
            {
                eq = Arrays.deepEquals((Object[]) obj1, (Object[]) obj2);
            }
            else if (obj1 instanceof byte[] && obj2 instanceof byte[]) //
            {
                eq = Arrays.equals((byte[]) obj1, (byte[]) obj2);
            }
            else if (obj1 instanceof short[] && obj2 instanceof short[]) //
            {
                eq = Arrays.equals((short[]) obj1, (short[]) obj2);
            }
            else if (obj1 instanceof int[] && obj2 instanceof int[]) //
            {
                eq = Arrays.equals((int[]) obj1, (int[]) obj2);
            }
            else if (obj1 instanceof long[] && obj2 instanceof long[]) //
            {
                eq = Arrays.equals((long[]) obj1, (long[]) obj2);
            }
            else if (obj1 instanceof char[] && obj2 instanceof char[]) //
            {
                eq = Arrays.equals((char[]) obj1, (char[]) obj2);
            }
            else if (obj1 instanceof float[] && obj2 instanceof float[]) //
            {
                eq = Arrays.equals((float[]) obj1, (float[]) obj2);
            }
            else if (obj1 instanceof double[] && obj2 instanceof double[]) //
            {
                eq = Arrays.equals((double[]) obj1, (double[]) obj2);
            }
            else if (obj1 instanceof boolean[] && obj2 instanceof boolean[]) //
            {
                eq = Arrays.equals((boolean[]) obj1, (boolean[]) obj2);
            }
            else
            {
                eq = false;
            }
            
            return eq;
        }
        catch (Exception e)
        {
            return false;
        }
    }
    
    /**
     * *********************************************************** <br>
     * *说明： <BR>
     *
     * @param map
     * @return <BR>
     * @int <BR>
     * @methods PubEnvUtil#getSize <BR>
     * @author LiBencheng <BR>
     * @date Created on 2021年7月28日 <BR>
     * @time 上午9:44:17 <BR>
     * @version 1.0 <BR>
     *          ************************************************************ <br>
     * @see <BR>
     */
    public static int getSize(Map<?, ?> map)
    {
        return (null == map ? 0 : map.size());
    }
    
    /**
     * *********************************************************** <br>
     * *说明： <BR>
     *
     * @param collection
     * @return <BR>
     * @int <BR>
     * @methods PubEnvUtil#getSize <BR>
     * @author LiBencheng <BR>
     * @date Created on 2021年7月28日 <BR>
     * @time 上午9:44:21 <BR>
     * @version 1.0 <BR>
     *          ************************************************************ <br>
     * @see <BR>
     */
    public static int getSize(Collection<?> collection)
    {
        return (null == collection ? 0 : collection.size());
    }
    
    /**
     * *********************************************************** <br>
     * *说明： <BR>
     *
     * @param objs
     * @return <BR>
     * @$ <BR>
     * @int <BR>
     * @methods PubEnvUtil#getSize <BR>
     * @author LiBencheng <BR>
     * @date Created on 2021年7月28日 <BR>
     * @time 上午9:45:53 <BR>
     * @version 1.0 <BR>
     *          ************************************************************ <br>
     */
    public static int getSize(Object[] objs)
    {
        return (null == objs ? 0 : objs.length);
    }
    
    /**
     * *********************************************************** <br>
     * *说明： <BR>
     *
     * @param model
     * @param entitys <BR>
     * @void <BR>
     * @methods PubEnvUtil.setModel <BR>
     * @author LiBencheng <BR>
     *         HXC067 <BR>
     * @date Created on 2021年7月28日 <BR>
     * @time 上午9:49:06 <BR>
     * @version 1.0 <BR>
     *          ************************************************************ <br>
     */
    public static void setModel(Model model, List<?> entitys)
    {
        
        model.addAttribute("temps", entitys);
        // 设置每页条数
        int pageSize = 20, pageNo = 0;
        // 页数
        // 设置最大页数
        int totalCount = PubEnvUtil.getSize(entitys);
        int maxPage = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1;
        if (pageNo > maxPage)
        {
            pageNo = maxPage;
        }
        model.addAttribute("pageNo", pageNo);
        model.addAttribute("totalCount", totalCount);
        model.addAttribute("maxPage", maxPage);
        
    }
    
    public static String getFailMsg(Object objBean)
    {
        LinkedHashMap map = new LinkedHashMap();
        map.put(MSG, "operation failed!");
        map.put("code", "401");
        map.put(DATA, objBean);
        
        return map2JosnStr(map);
    }
    
    public static String getErrorMsg(Object objBean)
    {
        LinkedHashMap map = new LinkedHashMap();
        map.put(MSG, "Operation error!");
        map.put(CODE, "500");
        map.put(DATA, objBean);
        
        return map2JosnStr(map);
    }
    
    public static String map2JosnStr(Map<?, ?> map)
    {
        return new Gson().toJson(map, Map.class);
    }
    
    public static String getSuccessMsg(Object objBean)
    {
        LinkedHashMap map = new LinkedHashMap();
        map.put(MSG, "Operation succeeded");
        map.put(CODE, "200");
        map.put(DATA, objBean);
        
        return map2JosnStr(map);
    }
    
    public static void setAttrByJson(Object obj, Map<?, ?> resultMap)
    {
        Iterator it = resultMap.keySet().iterator();
        while (it.hasNext())
        {
            String key = it.next().toString();
            String value = valueOf(resultMap.get(key));
            BeanHelper.setProperty(obj, key.toLowerCase(), value);
        }
    }
    
    public static String valueOf(Object obj)
    {
        return (null == obj) ? null : obj.toString();
    }
    
    public static boolean isEmptyObj(Object obj)
    {
        return (null == obj) ? true : false;
    }
    
    /**
     * *********************************************************** <br>
     * 说明： *
     * 
     * <pre>
     * StringUtils.isBlank(null)      = true
     * StringUtils.isBlank("")        = true
     * StringUtils.isBlank(" ")       = true
     * StringUtils.isBlank("bob")     = false
     * StringUtils.isBlank("  bob  ") = false
     * </pre>
     * 
     * <br>
     * @see <br>
     * @param cs
     * @return <br>
     * @boolean <br>
     * @methods pers.bc.utils.pub.StringUtil#isBlank <br>
     * @author LiBencheng <br>
     * @date Created on 2020-11-19 <br>
     * @time 14:28:00 <br>
     * @version 1.0 <br>
     *************************************************************          <br>
     */
    public static boolean isBlank(final CharSequence cs)
    {
        int strLen;
        if (cs == null || (strLen = cs.length()) == 0)
        {
            return true;
        }
        for (int i = 0; i < strLen; i++)
        {
            if (!Character.isWhitespace(cs.charAt(i)))
            {
                return false;
            }
        }
        return true;
    }
    
    public static void setAttributeValue(Map<String, String> map)
    {
        if (!isEmptyObj(map))
        {
            String dateStr = DateUtil.dateTime(new Date());
            map.put(SuperEntity.DR, "0");
            map.put(SuperEntity.TS, dateStr);
            map.put(SuperEntity.CREATIONTIME, dateStr);
            map.put(SuperEntity.MODIFYTIME, WAVE);
            map.put(SuperEntity.DEF01, WAVE);
            map.put(SuperEntity.DEF02, WAVE);
            map.put(SuperEntity.DEF03, WAVE);
            map.put(SuperEntity.DEF04, WAVE);
            map.put(SuperEntity.DEF05, WAVE);
        }
        // return bean;
    }
    
    public static void setAttributeValue(Object bean)
    {
        if (!PubEnvUtil.isEmptyObj(bean))
        {
            String dateStr = DateUtil.dateTime(new Date());
            BeanHelper.setProperty(bean, SuperEntity.DR, "0");
            BeanHelper.setProperty(bean, SuperEntity.TS, dateStr);
            BeanHelper.setProperty(bean, SuperEntity.CREATIONTIME, dateStr);
            BeanHelper.setProperty(bean, SuperEntity.DEF01, WAVE);
            BeanHelper.setProperty(bean, SuperEntity.DEF02, WAVE);
            BeanHelper.setProperty(bean, SuperEntity.DEF03, WAVE);
            BeanHelper.setProperty(bean, SuperEntity.DEF04, WAVE);
            BeanHelper.setProperty(bean, SuperEntity.DEF05, WAVE);
        }
        // return bean;
    }
    
    /**
     * *********************************************************** <br>
     * *说明： <BR>
     * @param bean <BR>
     * @void <BR>
     * @methods PubEnvUtil.setUpdateAttrValue <BR>
     * @author LiBencheng <BR>
     *         HXC067 <BR>
     * @date Created on 2021年7月28日 <BR>
     * @time 上午9:57:51 <BR>
     * @version 1.0 <BR>
     * @dmgtpf<BR>
     * @git_config <BR>
     * @see <BR>
     *************************************************************      <br>
     */
    public static void setUpdateAttrValue(Object bean)
    {
        if (!PubEnvUtil.isEmptyObj(bean))
        {
            String dateStr = DateUtil.dateTime(new Date());
            BeanHelper.setProperty(bean, SuperEntity.DR, "0");
            BeanHelper.setProperty(bean, SuperEntity.TS, dateStr);
            BeanHelper.setProperty(bean, SuperEntity.MODIFYTIME, dateStr);
        }
    }
    
}
